//
//  KuLiViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "KuLiViewController.h"
#import "UITextField+JKInputLimit.h"
#import "UITextView+JKInputLimit.h"
#import "SelMapDZViewController.h"
#import "MapSelPlaceViewController.h"
#import "KuLiView.h"
#import "SelMapDZViewController.h"
@interface KuLiViewController ()<UITextViewDelegate,MapSelPlaceViewControllerDelegate>{
    NSString *_lat;
    NSString *_lng;
    KuLiView *klView;
}


@property (nonatomic,copy)NSString *state;//标示

@end

@implementation KuLiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showNav:@"苦力"];
    [self loadUI];
    
}

- (void)loadUI{
    UIScrollView *sclView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    sclView.contentSize = CGSizeMake(ScreenWidth, 720);
    [self.view addSubview:sclView];
    
    klView = [[[NSBundle mainBundle] loadNibNamed:@"KuLiView" owner:nil options:nil] lastObject];
    klView.frame = CGRectMake(0, 0, ScreenWidth, 720);
    [sclView addSubview:klView];
    
    klView.textView.layer.cornerRadius = 5.0;
    klView.textView.layer.borderWidth = 0.5;
    klView.textView.layer.borderColor = COLOR_RGB(230, 230, 230, 1).CGColor;
    klView.textView.delegate = self;
    klView.textView.text = @"请填写";
    klView.textView.textColor = [UIColor grayColor];
    
    klView.view1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickQJ)];
    [klView.view1 addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickGO)];
    klView.view2.userInteractionEnabled = YES;
    [klView.view2 addGestureRecognizer:tap2];
    
    UIScrollView *scrViews = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 40, ScreenWidth-40, 60)];
    scrViews.contentSize = CGSizeMake(300, 60);
    [klView.headView addSubview:scrViews];
    
    for (NSInteger i = 0; i<self.array.count; i++) {
        NSDictionary *dic = self.array[i];
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"img"]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*60+i*10, 0, 60, 60)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"Avatar-sample-187"]];
        imageView.layer.cornerRadius = 30.0;
        imageView.layer.masksToBounds = YES;
        [scrViews addSubview:imageView];
        
    }
    //
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click3)];
    klView.userInteractionEnabled = YES;
    [klView.headView addGestureRecognizer:tap3];
    //呼叫苦力
    [klView.btnHJ addTarget:self action:@selector(clickTel) forControlEvents:UIControlEventTouchUpInside];
    
    klView.textName.jk_maxLength  = 8;
    klView.textPhone.jk_maxLength = 11;
    klView.textQJPhone.jk_maxLength   = 11;
    klView.textQJName.jk_maxLength    = 8;
    klView.textKG.jk_maxLength      = 10;
 
    
}
//
- (void)click3{
    SelMapDZViewController *map = [[SelMapDZViewController alloc] init];
    map.datas = self.array;
    [self.navigationController pushViewController:map animated:YES];
}

//取件地址
- (void)clickQJ{
    MapSelPlaceViewController *map = [[MapSelPlaceViewController alloc] init];
    map.delegate = self;
    [self.navigationController pushViewController:map animated:YES];
    _state = @"aaa";
}
//送往地址
- (void)clickGO{
    MapSelPlaceViewController *map = [[MapSelPlaceViewController alloc] init];
    map.delegate = self;
    [self.navigationController pushViewController:map animated:YES];
    _state = @"sss";
}
#pragma -mark MapSelPlaceViewControllerDelegate
-(void)sendText:(NSString *)text Province:(NSString *)proce City:(NSString *)city Area:(NSString *)area{
    
}

-(void)sendAddress:(NSString *)address Det:(NSString *)det{
    if ([_state isEqualToString:@"aaa"]) {
        klView.labQJDZ.text = det;
        klView.labQJJD.text = address;
    }else{
        klView.labSJDZ.text = det;
        klView.labSJJD.text = address;
    }
}

-(void)sendLat:(NSString *)lat sendLng:(NSString *)lng{
    if ([_state isEqualToString:@"aaa"]) {
        _lat = lat;
        _lng = lng;
    }
    
}

#pragma mark - UITextViewDelegate
-(void)textViewDidBeginEditing:(UITextView *)textView{
    
    if ([textView.text isEqualToString:@"请填写"]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}

-(void)textViewDidEndEditing:(UITextView *)textView{
//    NSInteger count = textView.text.length;
//    klView.labLenth.text = [NSString stringWithFormat:@"%li/100",count];
    if (textView.text.length < 1) {
        textView.text = @"请填写";
        textView.textColor = [UIColor grayColor];
    }
}

-(void)textViewDidChange:(UITextView *)textView{
    //限制长度
    NSInteger count = textView.text.length;
    klView.labLenth.text = [NSString stringWithFormat:@"%li/100",count];

    if (textView.text.length >= 100) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 100)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        return;
    }
}

//呼叫苦力
- (void)clickTel {
    if ([klView.labQJDZ.text isEqualToString:@"地址"]) {

        [LICGToast showWithText:@"请选择取件地址"];
        return;
    }else if (klView.textQJdet.text.length == 0){

        [LICGToast showWithText:@"请输入取件详细地址"];
        return;
    }else if (klView.textQJName.text.length == 0){

        [LICGToast showWithText:@"请输入取件人姓名"];
        return;
    }else if (klView.textQJPhone.text.length == 0){
        
        [LICGToast showWithText:@"请输入取件人电话"];
        return;
    }
    
    else if ([klView.labSJDZ.text isEqualToString:@"地址"]){

        [LICGToast showWithText:@"请选择送件地址"];
        return;
    }else if (klView.textSJdet.text.length == 0){

        [LICGToast showWithText:@"请输入送件详细地址"];
        return;
    }else if (klView.textName.text.length == 0){
        
        [LICGToast showWithText:@"请输入收货人姓名"];
        return;
    }else if ([self validateMobile:klView.textPhone.text] == 0){
        
        [LICGToast showWithText:@"收货人电话号码格式不正确"];
        return;
    }else if ([klView.textView.text isEqualToString:@"请填写"]){
        
        [LICGToast showWithText:@"请输入物品内容"];
        return;
    }else if (klView.textKG.text.length == 0){
        
        [LICGToast showWithText:@"请输入物品重量"];
        return;
    }
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setValue:_merid forKey:@"merchantid"];
    NSString *qjdz = [NSString stringWithFormat:@"%@%@",klView.labQJDZ.text,klView.labQJJD.text];
    [parmas setValue:qjdz forKey:@"quaddress"];//取件地址
    [parmas setValue:klView.textQJdet.text forKey:@"quhousnum"];//取件门牌号
    [parmas setValue:klView.textQJName.text forKey:@"quname"];
    [parmas setValue:klView.textQJPhone.text forKey:@"quphone"];
    
    NSString *sjdz = [NSString stringWithFormat:@"%@%@",klView.labSJDZ.text,klView.labSJJD.text];
    [parmas setValue:sjdz forKey:@"goaddress"];//送往地址
    [parmas setValue:klView.textSJdet.text forKey:@"gonum"];//送往门牌号
    [parmas setValue:klView.textName.text forKey:@"goname"];//收货人
    [parmas setValue:klView.textPhone.text forKey:@"gophone"];//收货人电话
    [parmas setValue:klView.textView.text forKey:@"remarks"];
    [parmas setValue:klView.textKG.text forKey:@"weight"];
    [parmas setValue:_lng forKey:@"longitude"];
    [parmas setValue:_lat forKey:@"latitude"];
    [parmas setValue:_name forKey:@"ordername"];
    NSMutableArray *arr = [NSMutableArray array];
    NSLog(@"aaaarray:%@   %@  %@",self.array,qjdz,sjdz);
    for (NSDictionary *dic in self.array) {
        NSString *nameid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
        [arr addObject:nameid];
    }
    NSString *str = [arr componentsJoinedByString:@","];
    NSString *aaa = [NSString stringWithFormat:@"%@,",str];
    [parmas setValue:aaa forKey:@"delivery"];//选择成员
    
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Coolie/upload",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"呼叫苦力res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        NSString *mssage = [responseObject objectForKey:@"mssage"];
        if ([error integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [LICGToast showWithText:mssage];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
